define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    require('../../../xsyBase');
    

    $.widget('rk.xsyWorkflowBar', $.rk.xsyBase, {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;


            elem.addClass('xsy_approval_bar')

            var entityType = me._getCommonData().entityType;
            me.userFunctions = me._getCommonData().userFunctions;
            me._loadData({entityType : entityType}, function (result){
                if(result.self.stageName){
                    opt.data = result;
                    me.showWorkflowBar();
                }else {
                    elem.hide();
                }
            });
        },

        _init : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
        },

        bindEventHandler : function(operAttrs){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var data = opt.data;


            var flowButton = elem.find('div.xsy_approval_bar_flow');
            var menu = elem.find('div.xsy_approval_wrap');
            flowButton.rkEasyMenu({
                menu:menu,
                openBy:'mouseover'
            });



            elem.off('click', '[act="workflowBar"]').on('click', '[act="workflowBar"]', function(){
                var info = $(this);
                var params = {};
                params.belongId = info.attr("belongId");
                params.objectId = info.attr("objectId");
                params.entityTypeId = info.attr("entityTypeId");
                params.approvalBindingName = info.attr("approvalBindingName");
                params.toTask = info.attr("toTask");
                params.sendMessage = info.attr("sendMessage");
                params.taskid = info.attr("taskId");
                params.instanceId = info.attr("instanceId");
                params.transitionId = info.attr("transitionId");
                params.btnName = info.attr("btnName");
                params.operAttrs = JSON.stringify(operAttrs);

                var confirmMsg = "是否【" + info.attr("btnName") + "】？";

                rk.confirm(confirmMsg, function(){
                    $.postJson('/json/crm_workflow/deal-workflow.action', params).done(function(json) {
                        if(json.status == 0){
                            $.msg("已成功提交",1);
                            setTimeout(function(){
                                window.location.reload();
                            }, 1000);
                        }
                        if(json.status == 300000){
                            $.msg(json.statusContent);
                        }
                        if(json.status == 213004){
                            $.msg("当前对象处于审批中，请先处理审批操作");
                        }
                        if(json.status == 200000){
                            rk.dialog({
                                title: '提示',
                                width: 570,
                                success: function (dialogElem,footerElem) {
                                    dialogElem.append('<div style="width: 100%;text-align: center;line-height: 20px;font-size: 14px;">此流程已被'+ json.userName + '办理。<br/>办理时间为：'+ json.operTime + '。</div>');
                                    footerElem.find('[act="save"]').show();
                                    footerElem.find('[act="save"]').click(function() {
                                        setTimeout(function(){
                                            window.location.reload();
                                        }, 500);
                                    });
                                },
                                footer: [{act:'save',text:rk.i18n('CRM_CORE_CONFIRM')}]
                            });
                        }
                    }).fail(function(def, code, message) {
                        def.status != 0 && $.msg(rk.i18n('REQUEST_ERROR'));
                    });
                });


            });

        },
        

        showWorkflowBar : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var data = opt.data.self;

            //data.transitionJson = $.parseJSON(data.transitionJson);
            elem.show();
            var tpl = require('platform/widgets/crm/workflow/xsyWorkflowBar/widget.tpl');
            data.userFunctions = me.userFunctions;
            elem.html(rk.templateText(tpl,data));
            //elem.find('a.xsy_workflow_operator').attr('title',elem.find('a.xsy_workflow_operator').html());
            me.bindEventHandler(data.operAttrs);

        }
    });
});
